package com.android.djlmapp.adapter.kuandai;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.djlmapp.R;
import com.android.djlmapp.holder.KuanDaiDiQuHolder;
import com.android.djlmapp.model.kaika.KaiKaHomeInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 宽带地区数据适配器
 */

public class KuanDaiDiQuAdapter extends RecyclerView.Adapter<KuanDaiDiQuHolder> {
    private LayoutInflater mInflater;
    private int temp = -1;//记录每次点击的按钮的Id
    private List<KaiKaHomeInfo.KdAdressBean.AreaListBean> list = new ArrayList<>();

    public KuanDaiDiQuAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }

    public void setList(List<KaiKaHomeInfo.KdAdressBean.AreaListBean> mList) {
        this.list = mList;
        if (this.list == null) {
            this.list = new ArrayList<>();
        }
        notifyDataSetChanged();
    }

    public void clear() {
        list.clear();
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public KuanDaiDiQuHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_diqu_num, parent, false);
        KuanDaiDiQuHolder kuanDaiDiQuHolder = new KuanDaiDiQuHolder(view);
        return kuanDaiDiQuHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull KuanDaiDiQuHolder holder, int position) {
        TextView kdDiQu = holder.itemView.findViewById(R.id.kd_diqu);

        kdDiQu.setText(list.get(position).getName());
        kdDiQu.setTag(position);
        kdDiQu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                temp = (int) view.getTag();
                mOnItemClickListener.onItemClick(list.get(position));
                notifyDataSetChanged();
            }
        });

        if (position == temp) {
            kdDiQu.setSelected(true);
        } else {
            kdDiQu.setSelected(false);
        }
    }

    @Override
    public int getItemCount() {
        if (list.size() > 0) {
            return list.size();
        } else {
            return 0;
        }
    }

    public OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(KaiKaHomeInfo.KdAdressBean.AreaListBean item);
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickListener = mOnItemClickLitener;
    }

}
